home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / PAGES.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  31KB  |  1,414 lines

  1. /*
  2.  * PAGES.CPP
  3.  * Modifications for Chapter 13
  4.  *
  5.  * Implementation of the CPages class.  See PAGEWIN.CPP for additional
  6.  * member functions.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. HWND        g_hDlgPrint=NULL;
  23. BOOL        g_fCancelPrint=FALSE;
  24.  
  25.  
  26. /*
  27.  * CPages:CPages
  28.  * CPages::~CPages
  29.  *
  30.  * Constructor Parameters:
  31.  *  hInst           HINSTANCE of the application we're in.
  32.  *  cf              UINT application clipboard format.
  33.  */
  34.  
  35. CPages::CPages(HINSTANCE hInst, UINT cf)
  36.     : CWindow(hInst)
  37.     {
  38.     m_pPageCur=NULL;
  39.     m_iPageCur=0xFFFF;  //Pages are 0 indexed, so this is one before that.
  40.     m_cPages=0;
  41.     m_hWndPageList=NULL;
  42.  
  43.     /*
  44.      * Initialize to 2.54cm*2.54cm which is a page with no space for anything,
  45.      * just margins.  2.54cm=.5 inches on each margin.
  46.      */
  47.     m_cx=LOMETRIC_PER_INCH;
  48.     m_cy=LOMETRIC_PER_INCH;
  49.  
  50.     m_xPos=0L;
  51.     m_yPos=0L;
  52.  
  53.     m_dwIDNext=0;
  54.     m_pIStorage=NULL;
  55.  
  56.     m_fDirty=FALSE;
  57.     m_cf=cf;
  58.  
  59.     m_fDragSource=FALSE;
  60.     m_fMoveInPage=FALSE;
  61.     m_fLinkAllowed=FALSE;
  62.  
  63.     m_fDragRectShown=FALSE;
  64.  
  65.     m_uScrollInset=GetProfileInt("windows", "DragScrollInset", DD_DEFSCROLLINSET);
  66.     m_uScrollDelay=GetProfileInt("windows", "DragScrollDelay", DD_DEFSCROLLDELAY);
  67.  
  68.     m_uHScrollCode=0;
  69.     m_uVScrollCode=0;
  70.  
  71.     m_fShowTypes=FALSE;
  72.     return;
  73.     }
  74.  
  75.  
  76. CPages::~CPages(void)
  77.     {
  78.     //Insure memory is cleaned up in list, and do final IStorage::Release
  79.     FIStorageSet(NULL, FALSE, FALSE);
  80.  
  81.     if (NULL!=m_hFont && !m_fSystemFont)
  82.         DeleteObject(m_hFont);
  83.  
  84.     if (NULL!=m_hWndPageList)
  85.         DestroyWindow(m_hWndPageList);
  86.  
  87.     //m_hWnd destroyed with the document.
  88.     return;
  89.     }
  90.  
  91.  
  92.  
  93. /*
  94.  * CPages::FIsDirty
  95.  *
  96.  * Purpose:
  97.  *  Tells the caller (document) if anything's happened to dirty us.
  98.  *
  99.  * Parameters:
  100.  *  None
  101.  *
  102.  * Return Value:
  103.  *  None
  104.  */
  105.  
  106. BOOL CPages::FIsDirty(void)
  107.     {
  108.     return m_fDirty;
  109.     }
  110.  
  111.  
  112.  
  113. /*
  114.  * CPages::FInit
  115.  *
  116.  * Purpose:
  117.  *  Instantiates a pages window within a given parent.  The
  118.  *  parent may be a main application window, could be an MDI child
  119.  *  window. We really do not care.
  120.  *
  121.  * Parameters:
  122.  *  hWndParent      HWND of the parent of this window
  123.  *  pRect           LPRECT that this window should occupy
  124.  *  dwStyle         DWORD containing the window's style flags.  Should
  125.  *                  contain WS_CHILD | WS_VISIBLE in typical circumstances.
  126.  *  uID             UINT ID to associate with this window
  127.  *  pv              LPVOID unused for now.
  128.  *
  129.  * Return Value:
  130.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  131.  */
  132.  
  133. BOOL CPages::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  134.     , UINT uID, LPVOID pv)
  135.     {
  136.     int     cy;
  137.  
  138.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPAGES
  139.         , SZCLASSPAGES, dwStyle, pRect->left, pRect->top
  140.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  141.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  142.  
  143.     if (NULL==m_hWnd)
  144.         return FALSE;
  145.  
  146.     /*
  147.      * Create the hidden listbox we'll use to track pages.  We give it
  148.      * the owner-draw style so we can just store pointers in it.  We have
  149.      * to set the parent to NULL such that the window hangs around after
  150.      * the pages window is destroyed so that we can clean up the
  151.      * memory stored in it from the CPages destructor.
  152.      */
  153.     m_hWndPageList=CreateWindow("listbox", "Page List", WS_POPUP | LBS_OWNERDRAWFIXED
  154.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, m_hInst, NULL);
  155.  
  156.     if (NULL==m_hWndPageList)
  157.         return FALSE;
  158.  
  159.     //Create a 14 point Arial font, or use the system variable font.
  160.     cy=MulDiv(-14, LOMETRIC_PER_INCH, 72);
  161.     m_hFont=CreateFont(cy, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  162.         , ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY
  163.         , VARIABLE_PITCH | FF_SWISS, "Arial");
  164.  
  165.     if (NULL==m_hFont)
  166.         {
  167.         m_hFont=(HFONT)GetStockObject(ANSI_VAR_FONT);
  168.         m_fSystemFont=TRUE;
  169.         }
  170.  
  171.     return TRUE;
  172.     }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. /*
  180.  * CPages::FIStorageSet
  181.  *
  182.  * Purpose:
  183.  *  Provides the document's IStorage to the pages for its own uses.
  184.  *  If this is a new storage, then we initalize it with streams we
  185.  *  want to always exists.  If this is an open, then we create
  186.  *  our page list from the PageList string we wrote before.
  187.  *
  188.  * Parameters:
  189.  *  pIStorage       LPSTORAGE to the new or opened storage.  If this is
  190.  *                  NULL then we just clean up and exit.
  191.  *  fChange         BOOL indicating is this was a Save As operation
  192.  *                  meaning that we have the structure already, we
  193.  *                  just need to change our value of m_pIStorage.
  194.  *  fInitNew        BOOL indicating if this is a new storage or one
  195.  *                  opened from a previous save.
  196.  *
  197.  * Return Value:
  198.  *  None
  199.  */
  200.  
  201. BOOL CPages::FIStorageSet(LPSTORAGE pIStorage, BOOL fChange, BOOL fInitNew)
  202.     {
  203.     DWORD           dwMode=STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  204.     HRESULT         hr;
  205.     LPPAGE          pPage;
  206.     BOOL            fRet=FALSE;
  207.     ULONG           cbRead;
  208.     PAGELIST        pgList;
  209.     LPSTREAM        pIStream;
  210.     LPMALLOC        pIMalloc;
  211.     LPDWORD         pdwID;
  212.     UINT            i;
  213.  
  214.     //If we're just changing saved roots, just open the current page again
  215.     if (fChange)
  216.         {
  217.         if (NULL==pIStorage)
  218.             return FALSE;
  219.  
  220.         m_pIStorage->Release();
  221.         m_pIStorage=pIStorage;
  222.         m_pIStorage->AddRef();
  223.  
  224.         FPageGet(m_iPageCur, &m_pPageCur, TRUE);
  225.         return TRUE;
  226.         }
  227.  
  228.     if (NULL!=m_hWndPageList)
  229.         {
  230.         //On new or open, clean out whatever it is we have.
  231.         for (i=0; i < m_cPages; i++)
  232.             {
  233.             //CHAPTER13MOD
  234.             if (FPageGet(i, &pPage, FALSE))
  235.                 pPage->Release();
  236.             //End CHAPTER13MOD
  237.             }
  238.  
  239.         SendMessage(m_hWndPageList, LB_RESETCONTENT, 0, 0L);
  240.         }
  241.  
  242.     if (NULL!=m_pIStorage)
  243.         m_pIStorage->Release();
  244.  
  245.     m_pIStorage=NULL;
  246.  
  247.     //If we're just cleaning up, then we're done.
  248.     if (NULL==pIStorage)
  249.         return TRUE;
  250.  
  251.     m_pIStorage=pIStorage;
  252.     m_pIStorage->AddRef();
  253.  
  254.     //If this is a new storage, create the streams we require
  255.     if (fInitNew)
  256.         {
  257.         //Page list header.
  258.         hr=m_pIStorage->CreateStream(SZSTREAMPAGELIST, dwMode | STGM_CREATE
  259.             , 0, 0, &pIStream);
  260.  
  261.         if (FAILED(hr))
  262.             return FALSE;
  263.  
  264.         pIStream->Release();
  265.  
  266.         //Device Configuration
  267.         hr=m_pIStorage->CreateStream(SZSTREAMDEVICECONFIG, dwMode | STGM_CREATE
  268.             , 0, 0, &pIStream);
  269.  
  270.         if (FAILED(hr))
  271.             return FALSE;
  272.  
  273.         pIStream->Release();
  274.         return TRUE;
  275.         }
  276.  
  277.  
  278.     /*
  279.      * We're opening an existing file:
  280.      *  1)  Configure for the device we're on
  281.      *  2)  Read the Page List and create page entries for each.
  282.      */
  283.  
  284.     ConfigureForDevice();
  285.  
  286.     //Read the page list.
  287.     hr=m_pIStorage->OpenStream(SZSTREAMPAGELIST, NULL, dwMode, 0, &pIStream);
  288.  
  289.     if (FAILED(hr))
  290.         return FALSE;
  291.  
  292.     if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  293.         {
  294.         pIStream->Read((LPVOID)&pgList, sizeof(PAGELIST), &cbRead);
  295.         m_cPages  =(UINT)pgList.cPages;
  296.         m_iPageCur=(UINT)pgList.iPageCur;
  297.         m_dwIDNext=pgList.dwIDNext;
  298.  
  299.         fRet=TRUE;
  300.         cbRead=pgList.cPages*sizeof(DWORD);
  301.  
  302.         if (0!=cbRead)
  303.             {
  304.             pdwID=(LPDWORD)pIMalloc->Alloc(cbRead);
  305.  
  306.             if (NULL!=pdwID)
  307.                 {
  308.                 pIStream->Read((LPVOID)pdwID, cbRead, &cbRead);
  309.  
  310.                 for (i=0; i < m_cPages; i++)
  311.                     fRet &=FPageAdd(-1, *(pdwID+i), FA